package com.panopset.flywheel.gui;

import static com.panopset.Util.*;

import javax.swing.JTextArea;

import com.panopset.flywheel.Command;
import com.panopset.flywheel.Flywheel;

/**
 *
 * @author Karl Dinwiddie
 */
public final class ScriptFileContainer {

    /**
     * Flywheel infrastructure.
     */
    private FlywheelInfrastruture infrastructure;

    /**
     * Edit text area.
     */
    private JTextArea textArea;

    /**
     * Resolving command.
     */
    private Command resolvingCommand;

    /**
     * Stepping thread.
     */
    private Thread stepper;

    /**
     * Flywheel instance.
     */
    private Flywheel flywheel;

    /**
     * Constructor.
     *
     * @param newFlywheel
     *            Flywheel instance to work with in GUI.
     */
    public void setFlywheel(final Flywheel newFlywheel) {
        flywheel = newFlywheel;
    }

    /**
     * Step over the next script command.
     */
    public void step() {
        if (!flywheel.isTargetDirectoryValid()) {
            return;
        }
        if (flywheel.getControl().isStepping()) {
            flywheel.getControl().setWaiting(false);
        } else {
            if (stepper != null) {
                throw new RuntimeException("Logic error");
            }
            flywheel.getControl().setStepping(true);
            flywheel.getControl().setWaiting(true);
            stepper = new Thread(new Runnable() {
                public void run() {
                    flywheel.exec();
                    stepper = null;
                }
            }, "Flywheel Stepping");
            stepper.start();
        }
    }

    /**
     * Execute next Flywheel command.
     */
    public void exec() {
        if (flywheel == null) {
            return;
        }
        if (flywheel.getControl().isStepping()) {
            flywheel.getControl().setStepping(false);
            flywheel.getControl().setWaiting(false);
        } else {
            flywheel.exec();
        }
    }

    /**
     * Load template.
     */
    public void load() {
        if (flywheel.isTargetDirectoryValid()) {
            getTextArea().setText(
                    flywheel.getTemplate().getStringLineSupplier().getText());
        } else {
            dspmsg(x("Please specify a valid script file and target."));
        }
    }

    /**
     * Quit.
     */
    public void quit() {
        getInfrastructure().quit();
    }

    /**
     * @return Flywheel debugging infrastructure.
     */
    public FlywheelInfrastruture getInfrastructure() {
        if (infrastructure == null) {
            infrastructure = new FlywheelInfrastruture(flywheel);
        }
        return infrastructure;
    }

    /**
     * Get text area.
     *
     * @return Text area.
     */
    public JTextArea getTextArea() {
        if (textArea == null) {
            textArea = new JTextArea();
        }
        return textArea;
    }

    /**
     * Save script file.
     */
    public void save() {
        if (flywheel.isTargetDirectoryValid()) {
            dspmsg(x("NOT implemented yet..."));
        } else {
            dspmsg(x("Script(s) NOT saved, due to invalid configuration."));
        }
    }

    /**
     * For debugging.
     *
     * @param command
     *            Resolving command.
     */
    public void setResolvingCommand(final Command command) {
        resolvingCommand = command;
    }

    /**
     * @return Currently resolving command.
     */
    public Command getResolvingCommand() {
        return resolvingCommand;
    }
}
